package com.tbsoft.ehr.service.pubField;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.tbsoft.ehr.dao.pubField.PubFieldDao;
import com.tbsoft.ehr.model.PublicField;

public class PubFieldServiceImpl implements PubFieldService {

	private PubFieldDao pubFieldDao;
	
	public PubFieldDao getPubFieldDao() {
		return pubFieldDao;
	}

	public void setPubFieldDao(PubFieldDao pubFieldDao) {
		this.pubFieldDao = pubFieldDao;
	}

	@Override
	public void addPubField(PublicField pf) {
		this.pubFieldDao.save(pf);
	}

	@Override
	public List<PublicField> getPubFieldByType(String type) {
		return this.pubFieldDao.getPubFieldByType(type);
	}

	@Override
	public List<PublicField> getPubFields() {
		return this.pubFieldDao.getPubFields();
	}

	@Override
	public List<String> getPubFieldTypes() {
		return this.getPubFieldDao().getPubFieldTypes();
	}

	@Override
	public Map<String, String> getPubFieldTypeMap() {
		return this.getPubFieldDao().getPubFieldTypeMap();
	}

	@Override
	public void deletePubField(PublicField pf) {
		this.getPubFieldDao().delete(pf);
	}

	@Override
	public PublicField getPubField(PublicField pf) {		
		return this.getPubFieldDao().getPubField(pf);
	}

	@Override
	public void updatePubField(PublicField pf) {
		this.pubFieldDao.update(pf);
	}

	/**
	 * 获取类型为key的公共字段map，每个value是一个以code为key的公共字段数字
	 */
	@Override
	public Map<String, Map> getPubFieldMap() {
		//获取所有的类型
		Object[] pubTypes = getPubFieldTypes().toArray();
		//key:fieldType,value:Map<code,PubField>
		Map<String, Map> pubFieldTypeMap = new HashMap<String, Map>();

		List<PublicField> pubFieldTypeLst = null;		
		for (int i = 0; i < pubTypes.length; i++) {
			pubFieldTypeLst = getPubFieldByType(pubTypes[i].toString());
			//使用TreeMap排序
			Map<String,PublicField> pubFieldMap = new TreeMap<String,PublicField>();
			for(PublicField pf:pubFieldTypeLst){
				pubFieldMap.put(pf.getCode(), pf);
			}						
			pubFieldTypeMap.put(pubTypes[i].toString(), pubFieldMap);
		}
		return pubFieldTypeMap;
	}

}
